package com.sl.mes.db.service.mm;

import com.sl.mes.db.dao.MmMoMapper;
import com.sl.mes.db.dao.MmPmoMapper;
import com.sl.mes.db.itf.mm.IMmMo;
import com.sl.mes.db.itf.oid.IdGenerator;
import com.sl.mes.db.util.CouponConstant;
import com.sl.mes.db.vo.*;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MmMoService implements IMmMo {
    private final MmPmo.Column[] result = new MmPmo.Column[]
            { MmPmo.Column.vbillcode ,MmPmo.Column.cpmohid};
    private final MmMo.Column[] result1 = new MmMo.Column[]
            { MmMo.Column.vbillcode ,MmMo.Column.cpmohid};

    private final MmMo.Column[] result2 = new MmMo.Column[]
            { MmMo.Column.vrowno ,MmMo.Column.cpmohid,MmMo.Column.vfree1,
                    MmMo.Column.vbillcode,MmMo.Column.nplanputnum};

    private final MmMo.Column[] result3 = new MmMo.Column[]
            { MmMo.Column.vrowno ,MmMo.Column.cpmohid,MmMo.Column.vfree1,
                    MmMo.Column.vbillcode,MmMo.Column.nplanputnum};


    @Resource
    private MmMoMapper mmMoMapper;

    @Resource
    private MmPmoMapper mmPmoMapper;

    /**
     * 查询根据id
     *
     * @param cpmohid
     * @return
     */
    @Override
    public MmPmo findParentVoById(String cpmohid) {
        return mmPmoMapper.selectByPrimaryKey(cpmohid);

    }

    @Override
    public List<MmMo> findChildrenVoById(String cpmohid) {
        MmMoExample example = new MmMoExample();
        MmMoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(cpmohid)) {
            criteria.andCpmohidEqualTo(cpmohid);
        }
        return mmMoMapper.selectByExample(example);
    }

    /**
     * 根据流程订单号进行查询
     * @param vbillcode
     * @return
     */
    @Override
    public MmPmo findParentVoByVbillcode(String vbillcode) {
        MmPmoExample example = new MmPmoExample();
        example.or().andVbillcodeEqualTo(vbillcode);
        MmPmo mmPmo = mmPmoMapper.selectOneByExampleSelective(example,result);
        return mmPmo;
    }


    @Override
    public List<MmMo> findChildrenVoByVbillcode(String vbillcode) {
        MmMoExample example = new MmMoExample();
        example.or().andVbillcodeEqualTo(vbillcode);
        return mmMoMapper.selectByExampleSelective(example,result1);

    }

    /**
     * 根据炉号进行查询
     * @param
     * @return
     */
    @Override
    public List<MmMo> findChildrenVoByVrowno(String vrowno) {
        MmMoExample example = new MmMoExample();
        example.or().andVrownoEqualTo(vrowno);
        return mmMoMapper.selectByExampleSelective(example,result2);

    }

    /**
     * 根据工作中心查询
     * @param cwkid
     * @return
     */
    @Override
    public List<MmMo> findChildrenVoByCwkid(String cwkid) {
        MmMoExample example = new MmMoExample();
        example.or().andCwkidEqualTo(cwkid);
        return mmMoMapper.selectByExampleSelective(example,result3);
    }


    /**
     * 添加生产订单
     *
     * @param mmPmo
     * @param lstMmMo
     */
    @Override
    public int addMmPom(MmPmo mmPmo, List<MmMo> lstMmMo) {
        int res = 0;
        try {
            res = mmPmoMapper.insert(mmPmo);
            for (int i = 0; i < lstMmMo.size(); i++) {
                MmMo itemVo = lstMmMo.get(i);
                mmMoMapper.insert(itemVo);
            }
        } catch (Exception e) {
            res = 0;
        }
        return res;
    }

    /**
     * 删除订单
     *
     * @param cpmohid
     */
    @Override
    public int deleteMmPomById(String cpmohid) {
        return mmPmoMapper.deleteByPrimaryKey(cpmohid);
    }

    @Override
    public int deleteMmMoById(String cpmohid) {
        MmMoExample example = new MmMoExample();
        MmMoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(cpmohid)) {
            criteria.andCpmohidEqualTo(cpmohid);
        }
        return mmMoMapper.deleteByExample(example);
    }

    /**
     * 修改订单
     *
     * @param
     * @return
     */
    public int updateMmPmo(MmPmo mmPmo) {
        return mmPmoMapper.updateByPrimaryKey(mmPmo);
    }

    @Override
    public int updateMmMo(MmMo mmMo) {
        return mmMoMapper.updateByPrimaryKey(mmMo);
    }

    /**
     * 查询所有的生产订单
     * @return
     */
    @Override
    public List<MmMo> selectAllMmMo() {
        MmMoExample example=new MmMoExample();
        return mmMoMapper.selectByExample(example);
    }



    @Override
    public int addMmMo(MmMo mmMo) {
        return mmMoMapper.insert(mmMo);
    }


}
